define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'store_id', title: __('Store_id')},
                        {field: 'staff_id', title: __('Staff_id')},
                        {field: 'store.store_name', title: __('Store.store_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'project.project_name', title: __('Project.project_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'project.price', title: __('Project.price'), operate:'BETWEEN'},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'project_id', title: __('Project_id')},
                        {field: 'order_status', title: __('Order_status'), formatter: Table.api.formatter.status, searchList: {'-1': __('取消'),0: __('未开始'), 1: __('服务中'), 2: __('已完成')}},
                        {field: 'date', title: __('Date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'num', title: __('Num')},
                        {field: 'last_name', title: __('Last_name'), operate: 'LIKE'},
                        {field: 'first_name', title: __('First_name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'cash_price', title: __('Cash_price'), operate:'BETWEEN'},
                        {field: 'swiping_card_price', title: __('Swiping_card_price'), operate:'BETWEEN'},
                        {field: 'swiping_card_tip_price', title: __('Swiping_card_tip_price'), operate:'BETWEEN'},
                        {field: 'gift_card_price', title: __('Gift_card_price'), operate:'BETWEEN'},
                        {field: 'gift_card_tip_price', title: __('Gift_card_tip_price'), operate:'BETWEEN'},
                        {field: 'pay_status', title: __('Pay_status'), formatter: Table.api.formatter.status, searchList: {1: __('已支付'), 0: __('未支付')}},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'is_hang', title: __('Is_hang'), formatter: Table.api.formatter.status, searchList: {1: __('是'), 0: __('否')}},
                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'edit',
                                    icon: 'fa fa-pencil',
                                    title: __('Edit'),
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    visible: function (row) {
                                        if(row.is_confirm == 0 && row.is_cancel == 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'confirm',
                                    text: __('确认订单'),
                                    confirm:'确认吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'order/confirm',
                                    visible:function(row){
                                        if(row.is_confirm == 0 && row.is_cancel == 0){
                                            return true;
                                        }
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/recyclebin' + location.search,
                pk: 'order_id',
                sortName: 'order_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'order_status', title: __('Order_status'), formatter: Table.api.formatter.status, searchList: {'-1': __('取消'),0: __('未开始'), 1: __('服务中'), 2: __('已完成')}},
                        {field: 'date', title: __('Date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'num', title: __('Num')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
